﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace RitS.Graphics
{
    static class GraphicsDrawTools
    {
        public static Image GenerateCanvas(List<List<ColoredGeometry>> Shapes, Rect CanvasSize, double Scale, Point Origin)
        {
            DrawingGroup totalGroup = new DrawingGroup();

            foreach (List<ColoredGeometry> singleObject in Shapes)
            {
                foreach (ColoredGeometry coloredShape in singleObject)
                {
                    totalGroup.Children.Add(new GeometryDrawing(Brushes.White, coloredShape.Color, coloredShape.Shape));
                }
            }

            double PhysicsPixelWidth = Origin.X + (CanvasSize.Width / Scale);
            double PhysicsPixelsHeight = Origin.Y + (CanvasSize.Height / Scale);

            Point topRight = CanvasToPhysics(Scale, Origin, CanvasSize.BottomRight);

            Geometry border = new LineGeometry(Origin, topRight);
            totalGroup.Children.Add(new GeometryDrawing(Brushes.Transparent, new Pen(Brushes.Transparent,  1), border));

            TransformGroup transforms = new TransformGroup();
            transforms.Children.Add(new ScaleTransform(Scale, -1 * Scale));
            transforms.Children.Add(new TranslateTransform(-Origin.X, -Origin.Y));

            totalGroup.Transform = transforms;

            totalGroup.ClipGeometry = new RectangleGeometry(new Rect(Origin, topRight));

            DrawingImage screenImage = new DrawingImage(totalGroup);
            screenImage.Freeze();

            Image finalImage = new Image();

            finalImage.Stretch = Stretch.None;

            finalImage.Source = screenImage;

            return finalImage;
        }

        public static Point CanvasToPhysics( double Scale, Point Origin, Point CanvasPoint)
        {
            Point newPoint = new Point();
            newPoint.X = Origin.X + (CanvasPoint.X / Scale);
            newPoint.Y = Origin.Y + (CanvasPoint.Y / Scale);

            return newPoint;
        }

    }

}
